package cn.hu.pet.controller;

import cn.hu.basic.domain.dto.PetDto;
import cn.hu.pet.service.IPetService;
import cn.hu.pet.domain.Pet;
import cn.hu.pet.query.PetQuery;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;


    /**
     * 保存和修改公用的
     * @param petDto 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody PetDto petDto, HttpServletRequest request) throws Exception {
        if ( petDto.getId() == null){
            petService.addByPetDto(petDto,request);
        }else{
            petService.updateByPetDto(petDto);
        }
        return AjaxResult.getInstance();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        petService.delete(id);
        return AjaxResult.getInstance();
    }

    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id) {
        return petService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list() {
        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query) {
        return petService.listByQuery(query);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        petService.onsale(ids);
        return AjaxResult.getInstance();
    }
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        petService.offsale(ids);
        return AjaxResult.getInstance();
    }
}
